# 多阶段构建 - 构建阶段
FROM maven:3.9.4-openjdk-17 AS builder

# 配置 Maven 使用国内镜像源
RUN echo '<?xml version="1.0" encoding="UTF-8"?>\
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"\
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">\
    <mirrors>\
        <mirror>\
            <id>aliyun</id>\
            <name>Aliyun Maven</name>\
            <url>https://maven.aliyun.com/repository/public</url>\
            <mirrorOf>central</mirrorOf>\
        </mirror>\
    </mirrors>\
</settings>' > /usr/share/maven/ref/settings.xml

WORKDIR /app

# 复制pom.xml
COPY pom.xml .

# 下载依赖
RUN mvn dependency:go-offline -B -s /usr/share/maven/ref/settings.xml

# 复制源代码
COPY src ./src

# 构建应用
RUN mvn clean package -DskipTests -s /usr/share/maven/ref/settings.xml

# 运行阶段 - 使用官方 OpenJDK 镜像
FROM openjdk:17-jdk-slim

# 设置工作目录
WORKDIR /app

# 从构建阶段复制jar文件
COPY --from=builder /app/target/*.jar app.jar

# 创建数据目录
RUN mkdir -p /app/data

# 暴露端口
EXPOSE 8080

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8080/actuator/health || exit 1

# 启动应用
ENTRYPOINT ["java", "-jar", "app.jar"] 